package com.honour.busserver.backup.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.honour.busserver.backup.base.ConfigData;
import com.honour.busserver.backup.base.ResultMsg;
import com.honour.busserver.backup.entity.BusBackConfig;
import com.honour.busserver.backup.entity.QuartzBean;
import com.honour.busserver.backup.mapper.BusBackConfigMapper;
import com.honour.busserver.backup.service.BusBackConfigService;
import com.honour.busserver.backup.utils.CronUtil;
import com.honour.busserver.backup.utils.QuartzUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BusBackConfigServiceImpl extends ServiceImpl<BusBackConfigMapper, BusBackConfig> implements BusBackConfigService {

    @Autowired
    private BusBackConfigMapper busBackConfigMapper;

    @Autowired
    private Scheduler scheduler;
    @Transactional
    public ResultMsg updateOrAddConfig(BusBackConfig busBackConfig){
        //删除旧的定时任务
        QueryWrapper<BusBackConfig> wrapper = new QueryWrapper<>();
        List<BusBackConfig> oldBusBackConfigs = busBackConfigMapper.selectList(wrapper);
        //删除旧记录
        busBackConfigMapper.delete(null);
        for (BusBackConfig m : oldBusBackConfigs) {
            QuartzUtils.deleteScheduleJob(scheduler,m.getJobName());
        }
        //新加定时任务
        QuartzBean quartzBean = new QuartzBean();
        quartzBean.setJobClass(ConfigData.JOBCLASS);
        String month = busBackConfig.getMonth();
        String week = busBackConfig.getWeek();
        String time = busBackConfig.getTime();
        if(StringUtils.isNotBlank(month)){
           Map<String,String> months =  CronUtil.myCron(time,month,"0");
            for (Map.Entry<String, String> m : months.entrySet()) {
                System.out.println("按月份：key:" + m.getKey() + " value:" + m.getValue());
                //根据勾选数据库循环创建任务
                for (int i = 0; i < busBackConfig.getDbs().length; i++) {
                    String jobName = "DBBACKDATA-MONTH-"+m.getKey()+"-"+busBackConfig.getDbs()[i];
                    busBackConfig.setJobName(jobName);
                    busBackConfig.setCron(m.getValue());

                    BusBackConfig config = new BusBackConfig();
                    config.setDb(busBackConfig.getDbs()[i]);
                    config.setLocation(busBackConfig.getLocation());
                    config.setMonth(busBackConfig.getMonth());
                    config.setWeek(busBackConfig.getWeek());
                    config.setTime(busBackConfig.getTime());
                    config.setCron(busBackConfig.getCron());
                    config.setJobName(busBackConfig.getJobName());
                    config.setReserved(busBackConfig.getReserved());
                    config.setPurge("false".equals(busBackConfig.getPurge()) ? "0":"1");
                    busBackConfigMapper.insert(config);
                    //设置定时任务
                    quartzBean.setJobName(jobName);
                    quartzBean.setCronExpression(m.getValue());
                    QuartzUtils.createScheduleJob(scheduler,quartzBean,busBackConfig.getLocation());
                }

            }
        }
        if(StringUtils.isNotBlank(week)){
            Map<String,String> weeks =  CronUtil.myCron(time,week,"1");
            for (Map.Entry<String, String> m : weeks.entrySet()) {
                //根据勾选数据库循环创建任务
                for (int i = 0; i < busBackConfig.getDbs().length; i++) {
                    System.out.println("按周：key:" + m.getKey() + " value:" + m.getValue());
                    String jobName = "DBBACKDATA-WEEK-"+m.getKey()+"-"+busBackConfig.getDbs()[i];
                    busBackConfig.setJobName(jobName);
                    busBackConfig.setCron(m.getValue());

                    BusBackConfig config = new BusBackConfig();
                    config.setDb(busBackConfig.getDbs()[i]);
                    config.setLocation(busBackConfig.getLocation());
                    config.setMonth(busBackConfig.getMonth());
                    config.setWeek(busBackConfig.getWeek());
                    config.setTime(busBackConfig.getTime());
                    config.setCron(busBackConfig.getCron());
                    config.setJobName(busBackConfig.getJobName());
                    config.setReserved(busBackConfig.getReserved());
                    config.setPurge("false".equals(busBackConfig.getPurge()) ? "0":"1");
                    busBackConfigMapper.insert(config);
                    //设置定时任务
                    quartzBean.setJobName(jobName);
                    quartzBean.setCronExpression(m.getValue());
                    QuartzUtils.createScheduleJob(scheduler,quartzBean,busBackConfig.getLocation());
                }

            }
        }
        return new ResultMsg(true,200,"更新备份设置成功");
    }


    public List<BusBackConfig> getConfigByJobName(String jobName){
        LambdaQueryWrapper<BusBackConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BusBackConfig::getJobName,jobName);
        List<BusBackConfig> busBackConfigs = busBackConfigMapper.selectList(wrapper);
        return busBackConfigs;
    }



}
